package cn.maxwell.product.service.impl;

import cn.maxwell.basic.service.IBaiduAiAuditService;
import cn.maxwell.basic.service.impl.BaseServiceImpl;
import cn.maxwell.basic.util.BusinessException;
import cn.maxwell.basic.util.LoginContext;
import cn.maxwell.pet.domain.Pet;
import cn.maxwell.pet.domain.PetDetail;
import cn.maxwell.product.domain.Product;
import cn.maxwell.product.domain.ProductDetail;
import cn.maxwell.product.domain.ProductOnlineAuditLog;
import cn.maxwell.product.mapper.ProductDetialMapper;
import cn.maxwell.product.mapper.ProductMapper;
import cn.maxwell.product.mapper.ProductOnlineAuditLogMapper;
import cn.maxwell.product.service.IProductService;
import cn.maxwell.org.domain.Employee;
import cn.maxwell.org.mapper.EmployeeMapper;
import cn.maxwell.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private ProductOnlineAuditLogMapper logMapper;

    @Autowired
    private ProductDetialMapper  detialMapper;

    @Override
    public void add(Product product) {
        Boolean textBoolen = baiduAiAuditService.textAudit(product.getName());
        if(textBoolen){
            //添加宠物信息到数据库t_pet表
            super.add(product);

            // 获取添加的宠物自增主键id
            Long petId = product.getId();
            // 获取宠物信息里面的宠物明细内容
            ProductDetail productDetail = product.getProductDetail();
            if(productDetail!=null){//宠物明细不为空就操作添加方法，为空就不对数据库t_detail进行操作
                //设置t_detail表里面的pet_id值
                productDetail.setProduct_id(petId);
                //添加宠物明细
                detialMapper.add(productDetail);
        }
        }else {
            throw new BusinessException("添加失败！非法内容！");
        }
    }


    @Override
    public void update(Product product) {
        Boolean textBoolen = baiduAiAuditService.textAudit(product.getName());
        if(textBoolen){
            super.update(product);
            Long pet_id = product.getId();
            ProductDetail productDetail = product.getProductDetail();
            if(productDetail!=null){
                productDetail.setProduct_id(pet_id);
                detialMapper.updateByProductId(productDetail);
            }
        }else {
            throw new BusinessException("修改失败！非法内容!");
        }
    }


    @Override
    public void delete(Long id) {
        super.delete(id);

        detialMapper.deleteByProductId(id);
    }

    /**
     * 服务上架
     * @param ids
     * @param request
     */
    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        //1.获取登录人信息
        //2.修改状态
        //3.添加上架审核日志
         //获取登录人信息
        Logininfo logininfo = (Logininfo) LoginContext.getLoginUser(request);
        //获取管理员信息,emplo表里面的logininfo_id与logininfo登录信息里面的id相对应。通过登录信息id可以查询出管理员id
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        Long employeeId = employee.getId();
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            //1.审核（百度AI）
            Boolean nameBoo = baiduAiAuditService.textAudit(product.getName());
            //Boolean imgBoo = baiduAiAuditService.imageAudit(product.getResources());
            if(nameBoo) {
                //2.审核通过-->修改状态（注意不要修改创建时间）
                //2.1 设置上架时间
                product.setOnsaletime(new Date());
                //2.2 设置状态
                product.setState(1);
                //2.3 对数据库进行修改
                productMapper.update(product);
                //3. +添加上架成功日志信息
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();

                productOnlineAuditLog.setProduct_id(product.getId());
                productOnlineAuditLog.setState(1);
                productOnlineAuditLog.setNote("上架成功");
                productOnlineAuditLog.setAudit_time(new Date());
                productOnlineAuditLog.setAudit_id(employeeId);

                logMapper.add(productOnlineAuditLog);

            }else {
                //3.审核不通过-->添加上架失败日志信息
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();

                productOnlineAuditLog.setProduct_id(product.getId());
                productOnlineAuditLog.setState(0);
                productOnlineAuditLog.setNote("审核失败");
                productOnlineAuditLog.setAudit_time(new Date());
                productOnlineAuditLog.setAudit_id(employeeId);

                logMapper.add(productOnlineAuditLog);
                throw new BusinessException("审核失败！");
            }
        }
    }

    /**
     * 服务下架
     * @param ids
     * @param request
     */
    @Override
    public void offsale(List<Long> ids, HttpServletRequest request) {
        //修改状态
        //下架时间（new一个当前系统时间）
        Date offsaletime = new Date();
        //多参数：@param：将下架时间和所要操作的id传到数据库的操作方法
        productMapper.offsale(offsaletime,ids);
    }
}
